package com.jjh.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.jjh.annation.TestTime;
import com.jjh.consts.Result;
import com.jjh.pojo.Dish;
import com.jjh.pojo.vo.DishVo;
import com.jjh.service.DishService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/dish")
public class DishController {

    @Resource
    private DishService dishService;


    /**
     * 条件分页查询
     * @param page
     * @param size
     * @param map
     * @return
     */
    @PostMapping("/page/{page}/{size}")
    public Result page(@PathVariable("page")Integer page,
                       @PathVariable("size")Integer size,
                       @RequestBody Map<String,Object> map){

        return dishService.page(page,size,map);
    }

    @PostMapping("/add")
    public Result addDish(@RequestBody DishVo dishVo) throws JsonProcessingException {
         return dishService.addDish(dishVo);
    }

    /**
     * 修改菜品
     * @param dishVo
     * @return
     * @throws JsonProcessingException
     */
    @PutMapping("/update")
    public Result updateDish(@RequestBody DishVo dishVo) throws JsonProcessingException {
        return dishService.updateDish(dishVo);
    }

    /**
     * 修改菜品售卖状态
     * @param status
     * @return
     */
    @GetMapping("/status/{id}/{status}")
    public Result changeStatus(@PathVariable("id")Integer id,@PathVariable("status")Integer status){
         return dishService.changeStatus(id,status);
    }

    /**
     * 获取菜品的详细信息，菜品，口味，标签，图片
     * @return
     */
    @GetMapping("/info/{id}")
    public Result getDishInfos(@PathVariable("id")Integer id){
        return dishService.getDishInfo(id);
    }

    /**
     * 根据id数组批量删除菜品
     * @param map
     * @return
     */
    @PostMapping("/delete")
    public Result deleteDishes(@RequestBody Map<String, List<Integer>> map){
        List<Integer> ids = map.get("ids");
        return dishService.deleteDishes(ids);
    }

    @TestTime
    @GetMapping("/getbycateid/{cateId}")
    public Result getByCateId(@PathVariable("cateId")Integer cateId){
        return dishService.getByCateId(cateId);
    }



}
